package de.pernpas.calendar.service;

import java.util.Date;
import java.util.List;
import java.util.logging.Logger;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Text;

import de.pernpas.calendar.PMF;
import de.pernpas.calendar.common.CalUtils;
import de.pernpas.calendar.entity.Event;

public final class EventService {
	
	private final static Logger log = Logger.getLogger(EventService.class.getName());
	private final static String EMAILRECEIPT = "stinky.nerdy@googlemail.com";
	
	@SuppressWarnings("unchecked")
	public static List<Event> getAllEvents(){
		return (List<Event>) PMF.get().getPersistenceManager().newQuery(Event.class).execute();
	}
	
	@SuppressWarnings("unchecked")
	private static List<Event> getAllEvents(PersistenceManager pm) {
		return (List<Event>) pm.newQuery(Event.class).execute();
	}
	
	@SuppressWarnings("unchecked")
	public static List<Event> getEvents(int month, int year){
		CalUtils cu = new CalUtils();
		Date beginDate = cu.getFirstMinuteInMonth(month, year);
		Date endDate = cu.getLastMinuteInMonth(month, year);
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query q = pm.newQuery(Event.class, "date >= beginDate && date < endDate");
		q.declareImports("import java.util.Date");
		q.declareParameters("Date beginDate, Date endDate");
		return (List<Event>) q.execute(beginDate, endDate);
	}
	
	@SuppressWarnings("unchecked")
	public static List<Event> getEvents(int day, int month, int year){
		CalUtils cu = new CalUtils();
		Date beginDate = cu.getFirstMinuteOfDay(day, month, year);
		beginDate = cu.addMinutes(beginDate, -1);
		Date endDate = cu.getLastMinuteOfDay(day, month, year);
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query q = pm.newQuery(Event.class, "date >= beginDate && date < endDate");
		q.declareImports("import java.util.Date");
		q.declareParameters("Date beginDate, Date endDate");
		return (List<Event>) q.execute(beginDate, endDate);
	}
	
	public static Event getEvent(String id){
		Event result = null;
		Key key = KeyFactory.createKey(Event.class.getSimpleName(), Long.parseLong(id));
		result = (Event) PMF.get().getPersistenceManager().getObjectById(Event.class, key);
		return result;
	}
	
	public static Event getEvent(PersistenceManager pm, Long id){
		Event result = null;
		Key key = KeyFactory.createKey(Event.class.getSimpleName(), id);
		result = (Event) pm.getObjectById(Event.class, key);
		return result;
	}

	public static void insertEvent(Event event) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			event.setModified(new Date());
			event.setCreated(new Date());
			event = pm.makePersistent(event);
			log.info("Neuen Event eingefügt");
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			pm.close();
		}
		MailService ms = new MailService();
		ms.sendMail(event.toString(), EMAILRECEIPT, "Neuer Event wurde eingetragen");
	}

	public static void deleteAll() {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		List<Event> eventList = getAllEvents(pm);
		pm.deletePersistentAll(eventList);
		pm.close();
		log.fine("Alle Events wurden gelöscht.");
	}

	public static Event updateEvent(long keyID, Date date,
			String location, String name, String description) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Event event = getEvent(pm, keyID);
		event.setDate(date);
		event.setLocation(location);
		event.setEventName(name);
		event.setDescription(new Text(description));
		event.setModified(new Date());
		pm.makePersistent(event);
		pm.close();
		MailService ms = new MailService();
		ms.sendMail(event.toString(), EMAILRECEIPT, "Event wurde aktualisiert");
		return event;
	}

	public static void delete(long keyID) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Event event = getEvent(pm, keyID);
		pm.deletePersistent(event);
		pm.close();
		MailService ms = new MailService();
		ms.sendMail(event.toString(), EMAILRECEIPT, "Event wurde gelöscht");
	}

}
